package com.niuxiaofa.newlaw.Activity;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.AdvisoryTagBean;
import com.niuxiaofa.newlaw.Bean.ExpertBean;
import com.niuxiaofa.newlaw.Bean.LocalCitiesBean;
import com.niuxiaofa.newlaw.Bean.LocalProvincesBean;
import com.niuxiaofa.newlaw.Bean.PickerScrollViewBean;
import com.niuxiaofa.newlaw.Network.WisdomLibraryPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.View.CommonPopWindow;
import com.niuxiaofa.newlaw.View.PickerScrollView;
import com.niuxiaofa.newlaw.adapter.ConnoisseurAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;


/**
 * 行家
 */
public class ConnoisseurActivity extends BaseActivity implements WisdomLibraryPresenter.interface_getLocalProvinces, CommonPopWindow.ViewClickListener, WisdomLibraryPresenter.interface_getLocalCities, WisdomLibraryPresenter.interface_advisoryTag, WisdomLibraryPresenter.interface_getExpert {
    int currentIndex = 1; //

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;

    @BindView(R.id.professional_types)
    LinearLayout professional_types;

    @BindView(R.id.region)
    LinearLayout region;


    @BindView(R.id.region_text)
    TextView region_text;


    @BindView(R.id.professional_text)
    TextView professional_text;

    PickerScrollView pickerScrollView, pickerScrollView2;

    List<PickerScrollViewBean> dtoList = new ArrayList<>();
    List<PickerScrollViewBean> dtoList2 = new ArrayList<>();

    List<AdvisoryTagBean.DataDTO> listFather = new ArrayList<>();
    List<AdvisoryTagBean.DataDTO> listChild = new ArrayList<>();

    List<ExpertBean.DataDTO.ListDTO> listDTOS = new ArrayList<>();
    int POPTYLE = 0;

    boolean advisoryTagParent = true;

    String cityCode = "";
    String tags = "";
    String tag = "";

    ConnoisseurAdapter connoisseurAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_connoisseur;
    }

    @Override
    protected void initData() {

        setTitle("全部");
        getExpert();
        connoisseurAdapter = new ConnoisseurAdapter(listDTOS, new ConnoisseurAdapter.Onclick() {
            @Override
            public void onclick(int c) {
                ExpertBean.DataDTO.ListDTO listBean=listDTOS.get(c);
                ToActivityUtil.toNextActivityClass(ConsultingActivity.class, "ExpertBean", listBean);
            }
        });

        RecyclerViewHelp.setRecycler(recyclerView, 10).setAdapter(connoisseurAdapter);


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                currentIndex = 1;
                getExpert();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                currentIndex++;
                getExpert();
            }
        });

        professional_types.setOnClickListener(e -> {
            POPTYLE = 0;
            WisdomLibraryPresenter.advisoryTag("", this);
            advisoryTagParent = true;
        });

        region.setOnClickListener(e -> {
            POPTYLE = 1;
            WisdomLibraryPresenter.getLocalProvinces(this);
        });


    }


    @Override
    public void getChildView(PopupWindow mPopupWindow, View view, int mLayoutResId) {

        pickerScrollView = view.findViewById(R.id.address);
        pickerScrollView.setData(dtoList);
        pickerScrollView.setSelected(0);


        //滚动监听
        pickerScrollView.setOnSelectListener(new PickerScrollView.onSelectListener() {
            @Override
            public void onSelect(PickerScrollViewBean name) {

                switch (POPTYLE) {
                    case 0:
                        WisdomLibraryPresenter.advisoryTag(name.getId(), ConnoisseurActivity.this);
                        break;
                    case 1:
                        Map map = new HashMap();
                        map.put("name", name.getText());
                        WisdomLibraryPresenter.getLocalCities(map, ConnoisseurActivity.this);
                        break;
                    case 2:

                        break;

                }

//                Map map = new HashMap();
//                map.put("name", name);
//                WisdomLibraryPresenter.getLocalCities(map);

            }
        });


        pickerScrollView2 = view.findViewById(R.id.pickerscrollview2);
        pickerScrollView2.setData(dtoList2);
        pickerScrollView2.setSelected(0);


        //完成按钮
        TextView wanchen = view.findViewById(R.id.wanchen);
        wanchen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();

                switch (POPTYLE) {
                    case 0:

                        tag = pickerScrollView.getSelect().getId();
                        tags = pickerScrollView2.getSelect().getId();
                        professional_text.setText(pickerScrollView.getSelect().getText() + "/" + pickerScrollView2.getSelect().getText());
                        break;
                    case 1:

                        cityCode = pickerScrollView2.getSelect().getId();
                        region_text.setText(pickerScrollView.getSelect().getText() + "/" + pickerScrollView2.getSelect().getText());
                        break;


                }
                getExpert();

            }
        });

    }


    public void getExpert() {

        Map map2 = new HashMap();
        map2.put("cityCode", cityCode);
        map2.put("tags", tags);
        map2.put("tag", tag);
        map2.put("pageSize", 20);
        map2.put("pageNum", currentIndex);
        currentIndex = 1;
        WisdomLibraryPresenter.getExpert(map2, ConnoisseurActivity.this);
    }


    /**
     * *省份
     *
     * @param u
     */
    @Override
    public void getResponse(LocalProvincesBean u) {
        if (u.getCode() == 1) {
            dtoList.clear();
            for (int i = 0; i < u.getData().size(); i++) {
                PickerScrollViewBean pickerScrollViewBean = new PickerScrollViewBean();
                pickerScrollViewBean.setId(u.getData().get(i).getCitycode());
                pickerScrollViewBean.setText(u.getData().get(i).getName());
                dtoList.add(pickerScrollViewBean);

            }
            CommonPopWindow.newBuilder()
                    .setView(R.layout.pop_picker_selector_bottom)
                    .setAnimationStyle(R.style.anim_menu_bottombar)
                    .setBackgroundDrawable(new BitmapDrawable())
                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(getContext(), 200))
                    .setViewOnClickListener(this)
                    .setBackgroundDarkEnable(true)
                    .setBackgroundAlpha(0.7f)
                    .setBackgroundDrawable(new ColorDrawable(999999))
                    .build(this)
                    .showAsBottom(recyclerView);


            Map map = new HashMap();
            map.put("name", u.getData().get(0).getName());
            WisdomLibraryPresenter.getLocalCities(map, ConnoisseurActivity.this);
        }
    }

    /**
     * 城市
     *
     * @param u
     */
    @Override
    public void getResponse(LocalCitiesBean u) {
        if (u.getCode() == 1) {
            dtoList2.clear();
            for (int i = 0; i < u.getData().size(); i++) {
                PickerScrollViewBean pickerScrollViewBean = new PickerScrollViewBean();
                pickerScrollViewBean.setId(u.getData().get(i).getCitycode());
                pickerScrollViewBean.setText(u.getData().get(i).getName());
                dtoList2.add(pickerScrollViewBean);
            }

            pickerScrollView2.setData(dtoList2);

//            CommonPopWindow.newBuilder()
//                    .setView(R.layout.pop_picker_selector_bottom)
//                    .setAnimationStyle(R.style.pop_animation)
//                    .setBackgroundDrawable(new BitmapDrawable())
//                    .setSize(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(getContext(), 300))
//                    .setViewOnClickListener(this)
//                    .setBackgroundDarkEnable(true)
//                    .setBackgroundAlpha(0.7f)
//                    .setBackgroundDrawable(new ColorDrawable(999999))
//                    .build(this)
//                    .showAsBottom(recyclerView);
        }
    }

    /**
     * 标签
     *
     * @param u
     */
    @Override
    public void getResponse(AdvisoryTagBean u) {
        if (u.getCode() == 1) {

            u.getData().addAll(u.getData());


            if (advisoryTagParent) {
                dtoList.clear();
                CommonPopWindow.newBuilder()
                        .setView(R.layout.pop_picker_selector_bottom)
                        .setAnimationStyle(R.style.anim_menu_bottombar)
                        .setBackgroundDrawable(new BitmapDrawable())
                        .setSize(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(getContext(), 200))
                        .setViewOnClickListener(this)
                        .setBackgroundDarkEnable(true)
                        .setBackgroundAlpha(0.7f)
                        .setBackgroundDrawable(new ColorDrawable(999999))
                        .build(this)
                        .showAsBottom(recyclerView);

                listFather.clear();
                listFather.addAll(u.getData());
                for (int i = 0; i < u.getData().size(); i++) {
                    PickerScrollViewBean pickerScrollViewBean = new PickerScrollViewBean();
                    pickerScrollViewBean.setId(u.getData().get(i).getId());
                    pickerScrollViewBean.setText(u.getData().get(i).getName());
                    dtoList.add(pickerScrollViewBean);


                }
                pickerScrollView.setData(dtoList);
                advisoryTagParent = false;


                WisdomLibraryPresenter.advisoryTag(u.getData().get(0).getId(), ConnoisseurActivity.this);


            } else {

                dtoList2.clear();
                listChild.clear();
                listChild.addAll(u.getData());


                PickerScrollViewBean pickerScrollViewBean = new PickerScrollViewBean();
                pickerScrollViewBean.setId("");
                pickerScrollViewBean.setText("无");
                dtoList2.add(pickerScrollViewBean);

                for (int i = 0; i < u.getData().size(); i++) {
                    PickerScrollViewBean pickerScrollViewBean2 = new PickerScrollViewBean();
                    pickerScrollViewBean2.setId(u.getData().get(i).getId());
                    pickerScrollViewBean2.setText(u.getData().get(i).getName());
                    dtoList2.add(pickerScrollViewBean2);
                }


                pickerScrollView2.setData(dtoList2);

            }

        }
    }

    @Override
    public void getResponse(ExpertBean u) {
        if (u.getCode() == 1) {

            if (refreshLayout != null) {
                refreshLayout.finishLoadMore();
                refreshLayout.finishRefresh();

            }

            if (currentIndex == 1) {
                listDTOS.clear();
            }

            listDTOS.addAll(u.getData().getList());

            connoisseurAdapter.notifyDataSetChanged();
        }
    }
}
